﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FavoritesViewModel.cs" company="R.V.Kasner">
//   Copyright © 2011 R.V.Kasner
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Owen.Terminal.ViewModel
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    using GalaSoft.MvvmLight.Command;

    using Microsoft.Practices.Prism.Events;

    using Owen.Communication.Service;
    using Owen.Terminal.Commons;
    using Owen.Terminal.Events;
    using Owen.Terminal.View;

    public sealed class FavoritesViewModel : ViewModelBase<IFavoritesView>, IFavoritesViewModel
    {
        private readonly IEventAggregator eventAggregator;

        private readonly ObservableCollection<ParameterViewModel> parameterItems;

        private readonly ICommandsViewModel commandsViewModal;

        private readonly VisualCommand[] visCommands;

        private bool isTransmit;

        public FavoritesViewModel()
        {
            this.commandsViewModal = new CommandsViewModel();

            this.parameterItems = new ObservableCollection<ParameterViewModel>
                {
                    new ParameterViewModel(
                        new Parameter(
                        "5", true, "int", "Параметр типа int", 4, 4, "aeo", 50, ParameterTypes.Int, true)), 
                    new ParameterViewModel(
                        new Parameter(
                        "5", true, "int", "Параметр типа sort", 4, 4, "se.a", 50, ParameterTypes.Int, true)), 
                };
        }

        public FavoritesViewModel(
            IFavoritesView view, IEventAggregator eventAggregator, ICommandsViewModel commandsViewModal)
            : base(view)
        {
            this.View.Selected += this.OnSelected;

            this.eventAggregator = eventAggregator;

            this.eventAggregator.GetEvent<ConnectChangeEvent>().Subscribe(
                cs => this.isTransmit = (cs & SignallerStates.Transmit) == SignallerStates.Transmit);

            this.commandsViewModal = commandsViewModal;
            this.parameterItems = new ObservableCollection<ParameterViewModel>();

            this.eventAggregator.GetEvent<FavoriteChangedEvent>().Subscribe(this.OnFavoriteChanged);
            this.eventAggregator.GetEvent<OpenParametersFileEvent>().Subscribe(this.OnOpenedParamFile);

            this.visCommands = new[]
                {
                    new VisualCommand
                        {
                            Title = "Циклический опрос", 
                            Command = new RelayCommand(this.CiclesReceive, () => !this.isTransmit)
                        }, 
                };
        }

        public IEnumerable<ParameterViewModel> ParameterItems
        {
            get
            {
                return this.parameterItems;
            }
        }

        public ICommandsViewModel CommandsViewModal
        {
            get
            {
                return this.commandsViewModal;
            }
        }

        private void OnOpenedParamFile(IEnumerable<ParameterViewModel> paramsvm)
        {
            this.parameterItems.Clear();
            foreach (var parameterViewModel in paramsvm)
            {
                this.parameterItems.Add(parameterViewModel);
            }
        }

        private void OnSelected()
        {
            this.eventAggregator.GetEvent<VisualCommandsChangeEvent>().Publish(this.visCommands);
        }

        private void OnFavoriteChanged(ParameterViewModel obj)
        {
            if (obj.IsFavorite)
            {
                this.parameterItems.Add(obj);
            }
            else
            {
                this.parameterItems.Remove(obj);
            }
        }

        private void CiclesReceive()
        {
            this.eventAggregator.GetEvent<FavoriteCicleReadEvent>().Publish(null);
        }
    }
}